package com.sleepcamel.resteasyConfigurator.components;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;

import com.sleepcamel.resteasyConfigurator.model.ListChooserModel;
import com.sleepcamel.resteasyConfigurator.util.StringCaseInsensitiveSort;

public class ComboListChooser extends Composite{

	public static final int LIST_HEIGHT = 120;
	public static final int WIDTH = 175;
	private ListChooserModel<String> listChooserModel;
	private ListViewer selectedListViewer;
	private ComboViewer comboViewer;
	
	

	public ComboListChooser(Composite parent, int style) {
		super(parent, style);
		setBackground(this);
		listChooserModel = new ListChooserModel<String>();
		createControl();
		pack();
	}

	private void setBackground(Control control) {
		control.setBackground(Display.getDefault().getSystemColor(
				SWT.COLOR_WHITE));
	}

	protected void createControl() {
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		layout.verticalSpacing = 0;
		setLayout(layout);
		
		comboViewer = new ComboViewer(this, SWT.NONE);
		comboViewer.setContentProvider(ArrayContentProvider.getInstance());
		comboViewer.setSorter(new StringCaseInsensitiveSort());
		comboViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent paramSelectionChangedEvent) {
				IStructuredSelection selection = (IStructuredSelection) paramSelectionChangedEvent.getSelection();
				if ( !selection.isEmpty() ){
					listChooserModel.addSelectedChoice((String) selection.getFirstElement());
					comboViewer.refresh();
					selectedListViewer.refresh();
				}
			}
		});
		Combo combo = comboViewer.getCombo();
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.widthHint = WIDTH;
		combo.setLayoutData(data);

		List selectedList = new List(this, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.widthHint = WIDTH;
		data.heightHint = LIST_HEIGHT;
		selectedList.setLayoutData(data);
		
		selectedListViewer = new ListViewer(selectedList);
		selectedListViewer.setContentProvider(ArrayContentProvider.getInstance());
		selectedListViewer.setSorter(new StringCaseInsensitiveSort());
		selectedListViewer.addDoubleClickListener(new IDoubleClickListener() {
			@Override
			public void doubleClick(DoubleClickEvent paramDoubleClickEvent) {
				IStructuredSelection selection = (IStructuredSelection) paramDoubleClickEvent.getSelection();
				if ( !selection.isEmpty() ){
					listChooserModel.removeSelectedChoice((String) selection.getFirstElement());
					comboViewer.refresh();
					selectedListViewer.refresh();
				}
			}
		});
	}

	protected void setChoices(java.util.List<String> allChoices, java.util.List<String> selectedChoices) {
		listChooserModel.setUnSelectedChoices(allChoices);
		listChooserModel.setSelectedChoices(selectedChoices);
		comboViewer.setInput(allChoices);
		selectedListViewer.setInput(selectedChoices);
	}
}
